Skip to content

fix: Resolve RAI reuse bug (#28329)#737

Merged
Roopan-Microsoft merged 5 commits into
dev-v4from
psl-bug-rai1
Dec 23, 2025
Merged

fix: Resolve RAI reuse bug (#28329)#737
Roopan-Microsoft merged 5 commits into
dev-v4from
psl-bug-rai1

Conversation

@NirajC-Microsoft

Copy link
Copy Markdown
Contributor

Purpose

This pull request enhances the agent lifecycle management by improving how agent IDs are resolved and reused, ensuring better integration with both the Projects SDK and AgentsClient. It also introduces more robust logic for saving and restoring agent state, and refactors how the project_client is handled throughout the agent initialization process.

Agent ID resolution and reuse improvements:

  • Added the resolve_agent_id async method to prefer resolving agent IDs via the Projects SDK (for RAI agents) with a fallback to the AgentsClient, including detailed logging for success and failure cases.
  • Updated the get_database_team_agent method to use resolve_agent_id, improving reliability when restoring agents and ensuring the correct client is used based on agent type.

Agent state saving enhancements:

  • Improved save_database_team_agent to only save when the agent ID has changed, reducing unnecessary writes and adding more informative logging.

Refactoring and initialization changes:

  • Refactored agent initialization to consistently accept and pass the project_client parameter, ensuring it is available where needed and removing duplicate retrievals. [1] [2] [3] [4] [5]
  • Adjusted the agent invocation logic to ensure the team agent is saved to the database whenever a valid agent ID is present during streaming, ensuring state consistency.

Does this introduce a breaking change?

  • Yes
  • No

How to Test

  • Get the code
git clone [repo-address]
cd [repo-name]
git checkout [branch-name]
npm install
  • Test the code

What to Check

Verify that the following are valid

  • ...

Other Information

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request enhances agent lifecycle management by improving agent ID resolution and reuse, particularly for RAI agents, while refactoring the initialization flow to consistently handle project_client throughout the codebase.

Key changes:

  • Introduced a new resolve_agent_id method that prioritizes the Projects SDK for RAI agents with fallback to AgentsClient, including comprehensive logging
  • Refactored agent initialization to pass project_client as a parameter, eliminating duplicate retrievals and ensuring consistency
  • Enhanced the save logic to check for ID changes before writing to the database, reducing unnecessary operations

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/backend/v4/magentic_agents/foundry_agent.py Moves project_client initialization before super().__init__() and passes it to parent, removes conditional save logic from _after_open, adds save call during streaming in invoke method
src/backend/v4/magentic_agents/common/lifecycle.py Adds project_client parameter to constructors, implements resolve_agent_id method for dual-client agent resolution, refactors get_database_team_agent to use new resolution logic with client preference based on agent type, enhances save_database_team_agent with ID change detection

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/backend/v4/magentic_agents/common/lifecycle.py Outdated
Comment on lines +303 to +304
if self._agent.chat_client.agent_id:
await self.save_database_team_agent()

This comment was marked as resolved.

Comment thread src/backend/v4/magentic_agents/common/lifecycle.py Outdated
NirajC-Microsoft and others added 3 commits December 23, 2025 09:04
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Roopan-Microsoft Roopan-Microsoft merged commit f9f6aab into dev-v4 Dec 23, 2025
4 checks passed
@Priyanka-Microsoft Priyanka-Microsoft deleted the psl-bug-rai1 branch December 25, 2025 09:26
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 4.0.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants